struct TreeNode
{
    int val;
    TreeNode *left;
    TreeNode *right;
};

class Solution {
public:
    bool hasPathSum(TreeNode* root, int targetSum) 
    {
        int num = 0;
        return dfs(root, targetSum, num);
    }

    bool dfs(TreeNode*root, int targetSum, int num)
    {
        if (root == nullptr)
        {
            return false;
        }

        num += root->val;
        if (root->left == nullptr && root->right == nullptr)
        {
            if (num == targetSum )
            {
                return true;
            }
            return false;
        }
        else
        {
            return dfs(root->left, targetSum, num) || dfs(root->right, targetSum, num);
        }
    }
};
